package com.mingqijia.gassafety.webserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mingqijia.gassafety.db.entity.ReceiverValve;
import com.mingqijia.gassafety.db.mapper.ReceiverValveMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.webserver.service.ReceiverValveService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 业务通知规则表 服务实现类
 * </p>
 *
 * @author li.shiying
 * @since 2023-03-23
 */
@Service
public class ReceiverValveServiceImpl extends ServiceImpl<ReceiverValveMapper, ReceiverValve> implements ReceiverValveService {

    @Override
    public void addReceiverValve(ReceiverValve receiverValve) {
        LambdaQueryWrapper<ReceiverValve> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ReceiverValve::getDeviceId,receiverValve.getDeviceId());
        List<ReceiverValve> list = this.list(queryWrapper);
        //BA-BF02，类型：市电款，供电方式：220V
        if (Constants.ZJ_RECEIVER_CODE_1.equals(receiverValve.getReceiverCode())) {
            receiverValve.setReceiverType("市电款");
            receiverValve.setReceiverPowerMode("220V");
        } else if (Constants.ZJ_RECEIVER_CODE_2.equals(receiverValve.getReceiverCode())) {
            //BA-BF03，类型：电池款，供电方式：3.6V 9000Ah锂电池
            receiverValve.setReceiverType("电池款");
            receiverValve.setReceiverPowerMode("3.6V 9000Ah锂电池");
        } else if (Constants.ZJ_RECEIVER_CODE_3.equals(receiverValve.getReceiverCode()) ||
                Constants.ZJ_RECEIVER_CODE_4.equals(receiverValve.getReceiverCode())) {
            //BA-BF03，类型：电池款，供电方式：3.6V 9000Ah锂电池
            receiverValve.setReceiverType("电池款");
            receiverValve.setReceiverPowerMode("3.6V 9000Ah锂电池");
        } else {
            return;
        }
        if (CollectionUtils.isEmpty(list)) {
            this.save(receiverValve);
        } else {
            receiverValve.setId(list.get(0).getId());
            receiverValve.setIsDeleted(0);
            this.updateById(receiverValve);
        }
    }
}
